//
//  BKGLFaFangZiYuanView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/25.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BKGLFaFangZiYuanView.h"
#import "SYZiYuanFaFangDateView.h"


@interface BKGLFaFangZiYuanView ()

@property (nonatomic, strong) SYNoLabel *nolab;
@property (nonatomic, strong) NSMutableArray *dataListArr;
@property (nonatomic, strong) SYZiYuanFaFangDateView *zydateView;


@end


@implementation BKGLFaFangZiYuanView

- (instancetype)initWithFrame:(CGRect)frame{
    if ([super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self initSubView];
        [self layout];
        self.tableView.tableFooterView = [UIView new];
    }
    return self;
}

- (void)requestDataListIdArr:(NSMutableArray *)listArr {
    //TOP先让接口通，所以下边先写了空
    NSString *applyIds = listArr.firstObject;
//    NSString *courseid = listArr.lastObject;
//    NSString *applyIds = @"";
    NSString *courseid = @"0";
    [MBProgressHUD mj_showMessage:nil toView:self];
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI fd_requestLessonTutorLessonApplyId:applyIds courseId:courseid date:@"" keyWord:@"" isOnLine:@"1" success:^(NSMutableArray *listArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        if (listArr.count == 0) {
            weakSelf.nolab.hidden = NO;
            weakSelf.tableView.hidden = YES;
        } else {
            weakSelf.dataListArr = listArr;
            [weakSelf.tableView reloadData];
        }
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        weakSelf.nolab.hidden = NO;
        weakSelf.tableView.hidden = YES;
    }];
}


- (void)initSubView{
    type = 0;
    [self addSubview:self.navView];
    [self addSubview:self.tableView];
    _nolab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self];
    
    [_nolab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(44);
        make.bottom.right.left.equalTo(self);
    }];
    
}
- (void)layout{
    [_navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0).offset(0);
        make.top.equalTo(self->_navView.mas_bottom);
        make.bottom.equalTo(@0);
    }];
}
- (BKGLFaFangZiYuanNavView *)navView{
    if (!_navView) {
        BKGLFaFangZiYuanNavView *view = [[BKGLFaFangZiYuanNavView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
        view.backgroundColor = MAINBACKCOLOR;
        view.titleLabe.text = @"发放资源";
        view.delegate = self;
        
        [view initOption];
        _navView = view;
    }
    return _navView;
}
- (void)backClick{
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
// http://apipad.31waiyu.com/home/NoticeService.svc/SoonCourse
- (void)submiteClick{
//    if (type == 0) {
////        [self addResourcesViewController];
//
//    }else if (type == 1){
    
    if (self.dataListArr.count == 0) {
        [MBProgressHUD zr_showError:@"暂无资源，无法添加"];
        [NSObject executeRunloop:^{
            [self backClick];
        } afterDelay:1];
        return;
    }
    
        [_faFangShiJianAlertView removeFromSuperview];
//        [MBProgressHUD showHUDAddedTo:self animated:YES];
        [_selectedResourceHeader removeFromSuperview];
        _zydateView = [[NSBundle mainBundle] loadNibNamed:@"SYZiYuanFaFangDateView" owner:nil options:nil].firstObject;
        [self addSubview:_zydateView];
        [_zydateView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self);
        }];
        __weak typeof(self) weakSelf = self;
        _zydateView.block = ^(NSString *fafangriqi, NSString *huishouriqi) {
            [weakSelf requestDataListSeaveFafang:fafangriqi huishou:huishouriqi];
        };
//    }
}

- (void)requestDataListSeaveFafang:(NSString *)fafang huishou:(NSString *)huishou {
    NSString *applyids = nil;
    for (SYBuKeGuLiModel *bkgl in self.dataListArr) {
        if (applyids.length == 0) {
            applyids = bkgl.bkid;
        } else {
            applyids = SYStringWithPickFormat(applyids, SYStringWithPickFormat(@",", bkgl.bkid));
        }
    }
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI fd_requestLessonTutorLessonLineSaveIsOnLine:@"1" applyIds:applyids grandDate:fafang recycleDate:huishou success:^(BOOL isSuccess) {
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:@"批准成功~!"];
        [NSObject executeRunloop:^{
            [self backClick];
        } afterDelay:1];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
    }];
}


- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        [tableView registerClass:[FaFangZiYuanCell class] forCellReuseIdentifier:@"FaFangZiYuanCell"];
        tableView.backgroundColor = [UIColor whiteColor];
        _tableView = tableView;
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    return 1;
    //TOP
    return self.dataListArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FaFangZiYuanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FaFangZiYuanCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[FaFangZiYuanCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FaFangZiYuanCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row%2 == 0) {
        cell.backgroundColor = [UIColor whiteColor];
    }else{
        cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    }
    cell.indexPath = indexPath;
//    cell.dataArray = @[@"14258",@"姓名",@"Lession One",@"1",@"线下",@"2018-12-01-10 18:00:00",@"ssss",@"hhhhh"];
    //TOP
        SYBuKeGuLiModel *bkgl = self.dataListArr[indexPath.row];
        cell.dataArray = @[bkgl.serial,bkgl.name,bkgl.courseName,bkgl.lesson,@"线上",bkgl.attendDate,@"",@""];
    [cell reloadViewLayout];
    cell.resourceBloxk = ^(NSIndexPath *index) {
        [self addResourcesViewControllerDataArr:bkgl.dataResourceArr];
    };
    cell.removeBlock = ^(NSIndexPath *index) {
    
        
    };
    return cell;
}
#pragma mark - 添加资源选择视图
- (void)addResourcesViewControllerDataArr:(NSMutableArray *)dataArr {
    _selectedResourceHeader = [[SelectedFaFangZiYuanAlertView alloc]initWithFrame:CGRectMake(0, 44, self.frame.size.width, self.frame.size.height)];
    [self addSubview:_selectedResourceHeader];
    [_selectedResourceHeader requestDataArr:dataArr];
    type = 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    _headerView  = [[FaFangZiYuanHeaderView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width-30, 44) andArray:@[@"学号",@"姓名",@"教材",@"课时",@"类型",@"申请时间",@"资源",@"选择"]];
    _headerView.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    return _headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    return mainView;
}
@end
